﻿@inject IHttpService _httpService

@inject IPopupService PopupService
@inject IUserService _userService

@inject NavigationManager NavigationManager


<CnGalWebSite.Components.Buttons.MasaButton Fab Icon="@(IsCollected?"mdi-basket":"mdi-basket-plus-outline ")" Tooltip="@(IsCollected?"已收集":"收集")" Class="@Class" OnClick="OnClickStar" />


@code {
    [Parameter]
    public long Id { get; set; }

    [Parameter]
    public EventCallback<bool> CollectionChanged { get; set; }

    [Parameter]
    public string Class { get; set; }

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool IsCollected;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var user = await authenticationStateTask;

            if (user.User.Identity.IsAuthenticated)
            {
                await GetUserIsCollected();

            }
        }
    }


    public async Task GetUserIsCollected()
    {
        try
        {
            var result = await _httpService.GetAsync<CheckPeripheryIsCollectedModel>( "api/peripheries/CheckPeripheryIsCollected/" + Id);

            if (result != null)
            {
                IsCollected = result.IsCollected;
                await CollectionChanged.InvokeAsync(IsCollected);
                StateHasChanged();
            }

        }
        catch (Exception ex)
        {

        }
    }

    private async Task OnClickStar()
    {
        //判断用户是否登录
        var user = await authenticationStateTask;
        if (user.User.Identity.IsAuthenticated)
        {
            if (IsCollected)
            {
                await UnCollected();
            }
            else
            {
                await Collect();

            }
        }
        else
        {
            _userService.Login();
        }

    }

    private async Task UnCollected()
    {
        try
        {
            Result obj = await _httpService.GetAsync<Result>("api/peripheries/UnCollectPeriphery/" + Id);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("取消拥有周边失败", obj.Error);
            }
            else
            {
                IsCollected = false;
                await CollectionChanged.InvokeAsync(IsCollected);
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "取消拥有周边失败");
        }
    }
    private async Task Collect()
    {
        try
        {
            Result obj = await _httpService.GetAsync<Result>("api/peripheries/CollectPeriphery/" + Id);

            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("拥有周边失败", obj.Error);
            }
            else
            {
                IsCollected = true;
                await CollectionChanged.InvokeAsync(IsCollected);

                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "拥有周边失败");
        }
    }
}
